package com.ruoyi.system.controller;

import java.util.List;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.UserAnswers;
import com.ruoyi.system.service.IUserAnswersService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 答题记录Controller
 * 
 * @author ruoyi
 * @date 2025-05-09
 */
@RestController
@RequestMapping("/system/answers")
public class UserAnswersController extends BaseController
{
    @Autowired
    private IUserAnswersService userAnswersService;

    /**
     * 查询答题记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:answers:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserAnswers userAnswers)
    {
        startPage();
        List<UserAnswers> list = userAnswersService.selectUserAnswersList(userAnswers);
        return getDataTable(list);
    }

    /**
     * 导出答题记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:answers:export')")
    @Log(title = "答题记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserAnswers userAnswers)
    {
        List<UserAnswers> list = userAnswersService.selectUserAnswersList(userAnswers);
        ExcelUtil<UserAnswers> util = new ExcelUtil<UserAnswers>(UserAnswers.class);
        util.exportExcel(response, list, "答题记录数据");
    }

    /**
     * 获取答题记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:answers:query')")
    @GetMapping(value = "/{recordId}")
    public AjaxResult getInfo(@PathVariable("recordId") Long recordId)
    {
        return success(userAnswersService.selectUserAnswersByRecordId(recordId));
    }

    /**
     * 新增答题记录
     */
    @PreAuthorize("@ss.hasPermi('system:answers:add')")
    @Log(title = "答题记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserAnswers userAnswers)
    {
        return toAjax(userAnswersService.insertUserAnswers(userAnswers));
    }

    /**
     * 修改答题记录
     */
    @PreAuthorize("@ss.hasPermi('system:answers:edit')")
    @Log(title = "答题记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserAnswers userAnswers)
    {
        return toAjax(userAnswersService.updateUserAnswers(userAnswers));
    }

    /**
     * 删除答题记录
     */
    @PreAuthorize("@ss.hasPermi('system:answers:remove')")
    @Log(title = "答题记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{recordIds}")
    public AjaxResult remove(@PathVariable Long[] recordIds)
    {
        return toAjax(userAnswersService.deleteUserAnswersByRecordIds(recordIds));
    }
}
